﻿using ConfExpert.Forms.Base;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using DevTeam;
using NewLand;
using System;
using System.Data;

namespace ConfExpert.Forms.Depo.Tasinma
{
    public partial class Cikis : XForm
    {

        Rapor.KumasIrsaliye xrIrsaliye;
        DataSet dataSource = null;
        NewLandBarcode newLand = null;

        string IrsaliyeNo
        {
            get { return biIrsaliyeNo.EditValue.ToStr(); }
            set { biIrsaliyeNo.EditValue = value; }
        }

        public Cikis()
        {
            InitializeComponent();
        }

        private void Cikis_Load(object sender, EventArgs e)
        {
            newLand = new NewLandBarcode(this);

            Bind();

            biCom1.EditValue = ConfExpert.Properties.Settings.Default.NewLand1;
            biCom2.EditValue = ConfExpert.Properties.Settings.Default.NewLand2;
            //Sıra önemli;
            this.biCom1.EditValueChanged += biCom_EditValueChanged;
            this.biCom2.EditValueChanged += biCom_EditValueChanged;


            if (biCom1.EditValue.ToInt() > 0)
                newLand.CihazEkle(1, biCom1.EditValue.ToInt());

            if (biCom2.EditValue.ToInt() > 0)
                newLand.CihazEkle(2, biCom2.EditValue.ToInt());

            if (newLand.Cihazlar.Count == 0)
                btnStart.Enabled = true;
            else
                btnStart.Enabled = false;

            newLand.BarcodeRead += cihaz_BarcodeRead;

            gv.FormatAsNumber(1, "SistemMetraj", "FizikselMetraj");
            gv.AddSummary("SistemMetraj", "FizikselMetraj");

            gv.MakeReadOnly();
            gv.EnableRowDelete();

            bbiIrsaliye.ItemClick += bbiIrsaliye_ItemClick;
        }

        private void Bind(bool refresh = false)
        {
            dataSource = ExecuteDataSet("GetRollsForDepoTransfer");
            if (refresh)
                gv.RefreshDataSource(dataSource);
            else
                gv.Init(dataSource.Tables[0]);

            gv.BestFitColumns();

            IrsaliyeNo = dataSource.Tables[1].Rows[0][0].ToStr();
        }

        private void btnStart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (biCom1.EditValue.ToInt() > 0)
                newLand.CihazEkle(1, biCom1.EditValue.ToInt());

            if (biCom2.EditValue.ToInt() > 0)
                newLand.CihazEkle(2, biCom2.EditValue.ToInt());

            btnStart.Enabled = false;
        }

        void cihaz_BarcodeRead(int Barcode, DateTime Tarih, int CihazNo)
        {
            DataTable dt = ExecuteDataTable("InsBarkod", Barcode, Tarih, CihazNo);
            if (dt.Rows.Count == 1)
            {
                dataSource.Tables[0].ImportRow(dt.Rows[0]);
                gv.BestFitColumns();
            }
        }

        void bbiIrsaliye_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();

            if (gv.RowCount == 0)
            {
                ShowErrorMessage("Top barkodu okutun!");
                return;
            }

            if (IrsaliyeNo == "")
            {
                ShowErrorMessage("İrsaliye numarası girin!");
                return;
            }
            xrIrsaliye = new Rapor.KumasIrsaliye(IrsaliyeNo, WhoAmI, "GetKumasIrsaliyeByDepoTransfer");

            xrIrsaliye.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
            xrIrsaliye.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
            xrIrsaliye.PrinterName = "SAMSUNG ML 2550";

            xrIrsaliye.ShowPreview();
        }
        void PrintingSystem_StartPrint(object sender, PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = 4;
            e.PrintDocument.PrinterSettings.Collate = false;
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            ExecuteNonQuery("createKumasIrsaliyeByDepoTransfer", IrsaliyeNo);

            Bind(true);

            xrIrsaliye.ClosePreview();
        }

        private void biCom_EditValueChanged(object sender, EventArgs e)
        {
            newLand.TumunuKapat();

            btnStart.Enabled = true;

            ConfExpert.Properties.Settings.Default.NewLand1 = biCom1.EditValue.ToInt();
            ConfExpert.Properties.Settings.Default.NewLand2 = biCom2.EditValue.ToInt();

            ConfExpert.Properties.Settings.Default.Save();
            ConfExpert.Properties.Settings.Default.Upgrade();
        }
    }
}